package com.xuan.task.service;

import com.alibaba.fastjson.JSON;
import com.xuan.task.Mapper.HomeworkExampaperMapper;
import com.xuan.task.Mapper.HomeworkStudentMapper;
import com.xuan.task.Mapper.UserHomeworkExampaperHistoryMapper;
import com.xuan.task.domain.HomeworkExampaperView;
import com.xuan.task.domain.HomeworkStudent;
import com.xuan.task.domain.HomeworkView;
import com.xuan.task.domain.UserHomeworkExampaperHistory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * FileName: UserStatisticServie
 * Author:   xuan zongjun
 * Date:     2020/8/24 14:01
 * Description:
 */

@Service
public class UserStatisticService {
    @Autowired
    HomeworkStudentMapper homeworkStudentMapper;
    @Autowired
    HomeworkExampaperMapper homeworkExampaperMapper;
    @Autowired
    UserHomeworkExampaperHistoryMapper userHomeworkExampaperHistoryMapper;

    //插入作业和练习的历史表中
    public void insertUserHomeworkExampaperHistory(HomeworkView homeworkView) {
        int homeworkId = homeworkView.getHomeworkId();
        String homeworkName = homeworkView.getHomeworkName();
        int type = homeworkView.getType();
        Date lastTime = homeworkView.getLastestTime();

        //循环列表，通过作业ID获取用户列表
        List<HomeworkStudent> homeworkStudentList = homeworkStudentMapper.getAllHomeworkStudentByHomeworkId( homeworkId );
        for (HomeworkStudent homeworkStudent : homeworkStudentList) {
            String userId = homeworkStudent.getUserId();
            //试卷
            List<HomeworkExampaperView> homeworkExampaperViewList1 = homeworkExampaperMapper.getHomeworkExampaperByHomeworkId( homeworkId );
            //如果作业完成，获取每个试卷答题时间和分数
            for (HomeworkExampaperView homeworkExampaperView : homeworkExampaperViewList1) {
                doExampaerView( homeworkExampaperView, userId, homeworkId );
            }
            //资源包
            List<HomeworkExampaperView> homeworkExampaperViewList2 = homeworkExampaperMapper.getHomeworkResourceBundleByHomeworkId( homeworkId );
            //如果作业完成，获取每个试卷答题时间和分数
            for (HomeworkExampaperView homeworkExampaperView : homeworkExampaperViewList2) {
                doResourceBundleView( homeworkExampaperView, userId, homeworkId );
            }
            //单词
            List<HomeworkExampaperView> homeworkVocabularyList = homeworkExampaperMapper.getHomeworkVocabularyByHomeworkId( homeworkId );
            for (HomeworkExampaperView homeworkExampaperView : homeworkVocabularyList) {
                doVocabularyView( homeworkExampaperView, userId, homeworkId );
            }
            //合并试卷、资源包、单词资源包列表
            homeworkExampaperViewList1.addAll( homeworkExampaperViewList2 );
            homeworkExampaperViewList1.addAll( homeworkVocabularyList );

            UserHomeworkExampaperHistory userHomeworkExampaperHistory = new UserHomeworkExampaperHistory();
            userHomeworkExampaperHistory.setHomeworkId( homeworkId );
            userHomeworkExampaperHistory.setUserId( userId );
            userHomeworkExampaperHistory.setHomeworkName( homeworkName );
            userHomeworkExampaperHistory.setCompleteTime( homeworkStudent.getCompleteTime() );
            userHomeworkExampaperHistory.setIsComplete( homeworkStudent.getIsComplete() );
            userHomeworkExampaperHistory.setHomeworkExampaperViewList( JSON.toJSONString( homeworkExampaperViewList1 ) );
            userHomeworkExampaperHistory.setCreateTime( new Date() );
            userHomeworkExampaperHistory.setType( type );
            userHomeworkExampaperHistory.setLastestTime( lastTime );

            userHomeworkExampaperHistoryMapper.insertUserHomeworkExampaperHistory( userHomeworkExampaperHistory );
        }

    }

    //处理试卷的用户、正确率、完成试题数 和 总的试题数
    private void doExampaerView(HomeworkExampaperView homeworkExampaperView, String userId, Integer homeworkId) {
        //获取得分和总分数
        String exampaperId = homeworkExampaperView.getExampaperId();
        //试卷中试题的总数
        int countExampaper = homeworkExampaperMapper.getCountByExampaperId( exampaperId );
        homeworkExampaperView.setResourceCount( countExampaper );
        //试题总分
        double totalScore = homeworkExampaperMapper.getExampaperTotalScore( exampaperId );
        homeworkExampaperView.setTotalScore( (int) totalScore );

        Map<String, Object> exampaperMap = homeworkExampaperMapper.getExampaperScoreByHomeworkIdAndUserIdAndExampaperId( homeworkId, userId, exampaperId );
        if (exampaperMap != null) {
            String batchId = exampaperMap.get( "batch_id" ).toString();
            double score = Double.parseDouble( exampaperMap.get( "score" ).toString() );
            homeworkExampaperView.setScore( (int) score );
            homeworkExampaperView.setBatchId( batchId );
            //获取答题时间
            int totalTime = homeworkExampaperMapper.getAnswerTimeByExampaperIdAndHomeworkId( homeworkId, exampaperId, batchId );
            homeworkExampaperView.setUserTime( totalTime / 60 );//获取的值单位为秒，转换为分钟

            //正确率
            if (totalScore > 0) {
                double rate = score / totalScore;
                homeworkExampaperView.setScoreRate( (int) (rate * 100) + "%" );
            } else {
                homeworkExampaperView.setScoreRate( "---" );
            }
            //获取试卷中试题完成的个数
            int completeCountExampaper = homeworkExampaperMapper.getCompleteCountByExampaperId( exampaperId, userId, batchId, homeworkId );
            homeworkExampaperView.setCompleteResourceCount( completeCountExampaper );
        }
    }


    //处理资源包的用户、正确率、完成试题数 和 总的试题数
    private void doResourceBundleView(HomeworkExampaperView homeworkExampaperView, String userId, Integer homeworkId) {
        //资源包ID
        String resourceBundleId = homeworkExampaperView.getExampaperId();
        //资源包中资源的个数
        int resourceCount = homeworkExampaperMapper.getCountByResourceBundleId( resourceBundleId );
        homeworkExampaperView.setResourceCount( resourceCount );
        //资源总分
        homeworkExampaperView.setTotalScore( 100 );
        Map<String, Object> resourceMap = homeworkExampaperMapper.getResourceBundleScoreByHomeworkIdAndUserIdAndExampaperId( homeworkId, userId, resourceBundleId );
        if (resourceMap != null) {
            String batchId = resourceMap.get( "batch_number" ).toString();
            //资源包总的得分
            double score = Double.parseDouble( resourceMap.get( "score" ).toString() );
            homeworkExampaperView.setBatchId( batchId );

            //计算资源包的平均分
            if (resourceCount > 0) {
                score = score / resourceCount;
                homeworkExampaperView.setScore( (int) score );//获取得分
                homeworkExampaperView.setScoreRate( (int) score + "%" );//正确率
            } else {
                homeworkExampaperView.setScore( 0 );
                homeworkExampaperView.setScoreRate( "---" );
            }

            //获取答题时间
            int totalTime = homeworkExampaperMapper.getAnswerTimeByExampaperIdAndHomeworkId( homeworkId, resourceBundleId, batchId );
            homeworkExampaperView.setUserTime( totalTime / 60 );//获取的值单位为秒，转换为分钟
            //完成的个数
            int completeResourceCount = homeworkExampaperMapper.getCompleteCountByResourceBundleId( resourceBundleId, userId, batchId, homeworkId );
            homeworkExampaperView.setCompleteResourceCount( completeResourceCount );
        }
    }


    /**
     * 处理单词
     *
     * @param homeworkExampaperView
     * @param userId
     * @param homeworkId
     */
    private void doVocabularyView(HomeworkExampaperView homeworkExampaperView, String userId, Integer homeworkId) {
        //资源包ID
        String resourceLibraryNum = homeworkExampaperView.getExampaperId();
        //资源包中单词的个数
        int vocabularyCount = homeworkExampaperMapper.getCountByResourceLibraryNum( resourceLibraryNum );
        homeworkExampaperView.setResourceCount( vocabularyCount );
        //资源总分
        homeworkExampaperView.setTotalScore( 100 );
        Map<String, Object> resourceMap = homeworkExampaperMapper.getVocabularyScoreByHomeworkIdAndUserIdAndResourceLibraryNum( homeworkId, userId, resourceLibraryNum );
        if (resourceMap != null) {
            String batchId = resourceMap.get( "batch_number" ).toString();
            //资源包总的得分
            double score = Double.parseDouble( resourceMap.get( "score" ).toString() );
            homeworkExampaperView.setBatchId( batchId );

            //计算资源包的平均分
            if (vocabularyCount > 0) {
                score = score / vocabularyCount;
                homeworkExampaperView.setScore( (int) score );//获取得分
                homeworkExampaperView.setScoreRate( (int) score + "%" );//正确率
            } else {
                homeworkExampaperView.setScore( 0 );
                homeworkExampaperView.setScoreRate( "---" );
            }

            //获取答题时间
            int totalTime = homeworkExampaperMapper.getAnswerTimeByExampaperIdAndHomeworkId( homeworkId, resourceLibraryNum, batchId );
            homeworkExampaperView.setUserTime( totalTime / 60 );//获取的值单位为秒，转换为分钟
            //完成的个数
            int completeResourceCount = homeworkExampaperMapper.getCompleteCountByResourceLibraryNum( resourceLibraryNum, userId, batchId, homeworkId );
            homeworkExampaperView.setCompleteResourceCount( completeResourceCount );
        }
    }





}
